<?php 

function dispatch() {
	$req = array_merge_recursive($_GET,$_POST);
	$q = isset($req["q"]) ? $req["q"] : "";
	$q = preg_replace('/\/+$/',"",$q);
	$tmp = explode("/",$q);
	$tmp[0] = empty($tmp[0]) ? "index" : $tmp[0];
	
	return array(
		"action" => $tmp[0],
		"params" => array_slice($tmp,1)
	);
}

function object_call_method(&$object, $method="index", $params=array()) {
	// build params
	$params = is_array($params) ? $params : array($params);
	foreach ($params as &$param) {
		if(!preg_match('/^\d+$/', $param)) {
			$param = "'".str_replace("'","\\'",$param)."'";
		}
	}
	$ret = false;
	$toeval = '$ret = $object->'.$method.'('.implode(",",$params).');';
	$status = @eval($toeval);
	if($status === false) {
		header("X-Eval-String: $toeval");
		die("Error evaluating.");
	}
	return $ret;
}

?>